﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Security.Cryptography;
using System.Collections.Specialized;
using System.IO;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Net;
using Cosmetic;
using Cosmetic.DataEntity;
using Cosmetic.Logic;
using System.Collections.Generic;
using UdpaySecurity;


namespace WebApp.Shopping
{
    public partial class receive : System.Web.UI.Page
    {

        PurchaseManager pm = new PurchaseManager();


        OrderManager m_orderManger = new OrderManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            string desKey = "A2F167FDBCBF2A0D70B0FDD5C81CF8C71991409D2C2F1CE3";
            string txCode = Request["txCode"];
            string merchantId = Request["merchantId"];
            string stlmDate = Request["stlmDate"];
            string transFlow = Request["transFlow"];
            string orderResult = Request["orderResult"];
            string orderNo = Request["orderNo"];
            string whtOrderNo = Request["whtOrderNo"];
            string amount = Request["amount"];
            string transAmount = Request["transAmount"];
            string fee = Request["fee"];
            string orderTxt = Request["orderTxt"];
            string sign = Request["sign"];

            /*ThreeDES解密结果，用做记录相关数据
            String decFee = ThreeDES.ThreeDES.Decrypt3DES(fee, desKey);
            String decAmount = ThreeDES.ThreeDES.Decrypt3DES(amount, desKey);
            String decTransAmount = ThreeDES.ThreeDES.Decrypt3DES(transAmount, desKey);
            String decOrderTxt = ThreeDES.ThreeDES.Decrypt3DES(orderTxt, desKey);
            */

            StringBuilder sb = new StringBuilder();
            sb.Append("txCode=" + txCode);
            sb.Append("&merchantId=" + merchantId);
            sb.Append("&stlmDate=" + stlmDate);
            sb.Append("&transFlow=" + transFlow);
            sb.Append("&orderResult=" + orderResult);
            sb.Append("&orderNo=" + orderNo);
            sb.Append("&whtOrderNo=" + whtOrderNo);
            sb.Append("&amount=" + amount);
            sb.Append("&transAmount=" + transAmount);
            sb.Append("&fee=" + fee);
            sb.Append("&orderTxt=" + orderTxt);

            string resule = sb.ToString();

            //装载解析密钥
            UdpaySecurity.UdpayRsa.LoadSignKey();
            //如果为0代表验证成功，-1代表验证失败；
            int vresule = UdpaySecurity.UdpayRsa.VerifySigature(resule, sign);

            if (vresule==0)
            {
                Order theOrder = pm.GetOrderByTradeNo(orderNo);
                theOrder.Valid = true;
                pm.UpdateOrder(orderNo);
            }
            sb = new StringBuilder();

            string receiveStr = sb.Append("txCode=").Append(txCode).Append("&merchantId=").Append(merchantId).Append("&stlmDate=").Append(stlmDate).Append("&transFlow=").Append(transFlow)
                                        .Append("&orderResult=").Append(orderResult).Append("&orderNo=").Append(orderNo).Append("&whtOrderNo=").Append(whtOrderNo)
                                        .Append("&amount=").Append(amount).Append("&transAmount=").Append(transAmount).Append("&fee=").Append(fee).Append("&success=Y&errorMsg=").ToString();
            string receiveSrcStr = receiveStr + "&sign=" + UdpayRsa.GenerateSigature(receiveStr);
            //按照返回格式打印返回信息
            Response.Write(receiveSrcStr);
        }
    }
}